use axum::{async_trait, extract::FromRequestParts, http::request::Parts};

use crate::{error::{ec, new_err_msg, AppErr, ErrorWrap}, server::header};




pub struct DeviceInfo {
    pub id: u64,
}


#[async_trait]
impl<S: Send + Sync> FromRequestParts<S> for DeviceInfo {
    type Rejection = AppErr;

    async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
        let id: u64 = parts
            .headers
            .get(header::DEVICE_TOKEN)
            .ok_or(new_err_msg(ec::DEVICE_NO_TOKEN, ec::DEVICE_NO_TOKEN_MSG))?
            .to_str()
            .wrap()?
            .parse()
            .wrap()?;
        Ok(DeviceInfo { id })
    }
}



